package cn.valo.basic.controller;

import cn.valo.basic.dto.LoginDto;
import cn.valo.basic.dto.WeChatDto;
import cn.valo.basic.exception.BusinessException;
import cn.valo.basic.service.ILoginService;
import cn.valo.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;
    //登陆接口/前后台共用
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            //登录成功之后，service返回token  + logininfo
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.success().setResultObj(map);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
    @PostMapping("/verify")
    public AjaxResult verify(HttpServletRequest request){
        String token = request.getHeader("U-TOKEN");
        try {
            return AjaxResult.success().setMsg("获取登陆信息成功！").setResultObj(loginService.verify(token));
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }


    /**
     * 微信登录接口
     * @param dto
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WeChatDto dto){
       try {
           return loginService.wechat(dto);
       } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
           e.printStackTrace();
           return AjaxResult.error().setSuccess(false).setMsg(e.getMessage());
      } catch (Exception e) {
           e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
       }
   }

    /**
     * 微信绑定接口
     * @param dto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WeChatDto dto){
        try {
            //登录成功之后，service返回token  + logininfo
            Map<String, Object> map = loginService.binder(dto);
            return AjaxResult.success().setResultObj(map);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
}